package com.mm.dss.demo;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.adapter.Myhttpadapter;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.QfQcBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;
import com.mm.dss.demo.view.LogoutDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.serialization.SoapObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.mm.dss.demo.tools.Constances.imgulr;

/*
 * 群防群测任务详情
 * */
public class QfqcInfoActivity extends BaseActivity {
    TextView txt_title;
    RelativeLayout layout_back;
    Button btn;
    TextView taskname;//任务名称
    TextView xfr;
    TextView time;
    TextView taskinfo;//详情
    TextView taskzhangtai;//任务状态
    TextView right;

    TextView taskOKName, taskOK;
    private LinearLayout lineartuOK;
    private RecyclerView img_recycler_view_ok;//

    public static Activity activity;
    private LinearLayout lineartu;//图片总的布局，默认隐藏，当处于已经完成的任务且有图片的情况下显示出来展示图片
    private String img = "";
    private String imgOk = "";
    private RecyclerView img_recycler_view;//

    private Dialog mWeiboDialog;
    private String ZT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qfqc_info);
        taskinfo = (TextView) findViewById(R.id.taskinfo);
        xfr = (TextView) findViewById(R.id.xfr);
        time = (TextView) findViewById(R.id.time);
        taskname = (TextView) findViewById(R.id.taskname);
        btn = (Button) findViewById(R.id.btn);
        layout_back = (RelativeLayout) findViewById(R.id.layout_back);
        txt_title = (TextView) findViewById(R.id.txt_title);
        txt_title.setText("任务详情");
        activity = this;
        layout_back.setVisibility(View.VISIBLE);
        lineartu = (LinearLayout) findViewById(R.id.lineartu);
        img_recycler_view = (RecyclerView) findViewById(R.id.img_recycler_view);
        taskzhangtai = (TextView) findViewById(R.id.taskzhangtai);
        taskOKName = (TextView) findViewById(R.id.taskOKName);
        taskOK = (TextView) findViewById(R.id.taskOK);
        lineartuOK = (LinearLayout) findViewById(R.id.lineartuOK);
        img_recycler_view_ok = (RecyclerView) findViewById(R.id.img_recycler_view_ok);
        right = (TextView) findViewById(R.id.right);
        right.setText("删除");
        right.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initdata(true);
    }

    protected void initdata(boolean IsDialog) {

        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(QfqcInfoActivity.this,
                            "加载中...");
        }
        Map<String, String> map = new HashMap<>();
        map.put("id", getIntent().getStringExtra("rwid"));
        WebServiceUtils.callWebService(Constances.webhosturl, "N_ReciveQCQF", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                try {
                    JSONArray array = new JSONArray(result.getProperty(0).toString());
                    Gson gson = new Gson();
                    QfQcBean qfqcBean = gson.fromJson(array.optJSONObject(0).toString(), QfQcBean.class);

                    taskname.setText("任务名称：" + qfqcBean.getKSMC());
                    time.setText("时  间：" + qfqcBean.getXCRQ());
                    taskinfo.setText(qfqcBean.getWCSJ());
                    img = qfqcBean.getIMG() + "";
                    imgOk = qfqcBean.getIMAGE() + "";
                    ZT=qfqcBean.getZT();
                    img_recycler_view.setLayoutManager(new GridLayoutManager(QfqcInfoActivity.this, 9));
                    img_recycler_view_ok.setLayoutManager(new GridLayoutManager(QfqcInfoActivity.this, 9));
                    switch (Integer.parseInt(qfqcBean.getZT())) {
                        case 0:
                            taskzhangtai.setText("任务状态： 未接受");
                            btn.setText("接受");
                            lineartu.setVisibility(View.GONE);
                            btn.setVisibility(View.VISIBLE);
                            xfr.setText("下发人： " + qfqcBean.getXCRY());
                            taskOKName.setVisibility(View.GONE);
                            taskOK.setVisibility(View.GONE);
                            taskinfo.setText(qfqcBean.getCJWT());
                            break;
                        case 1:
                            taskzhangtai.setText("任务状态： 未完成");
                            btn.setText("完成");
                            xfr.setText("接收人： " + qfqcBean.getXFRY());
                            if (qfqcBean.getXFRY().equals(SharedPreferencedUtils.getString(QfqcInfoActivity.this, "username"))) { //判断接收人是不是自己
                                btn.setVisibility(View.VISIBLE);
                            } else {
                                btn.setVisibility(View.GONE);
                            }
                            taskinfo.setText(qfqcBean.getCJWT());
                            taskOKName.setVisibility(View.GONE);
                            taskOK.setVisibility(View.GONE);

                            break;
                        case 2:
                            taskzhangtai.setText("任务状态： 已完成");
                            btn.setVisibility(View.GONE);
                            xfr.setText("接收人： " + qfqcBean.getXFRY());
                            taskOKName.setVisibility(View.VISIBLE);
                            taskOK.setVisibility(View.VISIBLE);
                            taskOK.setText(qfqcBean.getXCQK());
                            taskinfo.setText(qfqcBean.getCJWT());
                            break;
                        case 3:
                            taskzhangtai.setText("任务状态： 未下发");
                            btn.setText("下发");
                            btn.setVisibility(View.VISIBLE);
                            xfr.setText("采集人： " + qfqcBean.getXCRY());
                            taskOKName.setVisibility(View.GONE);
                            taskOK.setVisibility(View.GONE);
                            taskinfo.setText(qfqcBean.getCJWT());

                            if (SharedPreferencedUtils.getString(QfqcInfoActivity.this, "Level").equals("1")) {
                                right.setVisibility(View.VISIBLE);
                            }
                            break;
                    }

                    if (!TextUtils.isEmpty(img)) {
                        lineartu.setVisibility(View.VISIBLE);
                        final String[] sourceStrArray = img.split(",");
                        for (int i = 0; i < sourceStrArray.length; i++) {
                            sourceStrArray[i] = imgulr + sourceStrArray[i];
                            System.out.println(sourceStrArray[i]);
                            Log.i("img的值", imgulr + sourceStrArray[i] + "");
                        }
                        Myhttpadapter myadapter = new Myhttpadapter(sourceStrArray, QfqcInfoActivity.this);
                        img_recycler_view.setAdapter(myadapter);
                        myadapter.setItemListener(new Myhttpadapter.onRecyclerItemClickerListener() {
                            @Override
                            public void onItemClick(View view, String[] strlist, int i) {
                                Intent intent = new Intent();
                                intent.putExtra("imageUrls", sourceStrArray);
                                intent.putExtra("curImageUrl", sourceStrArray[i]);
                                intent.putExtra("tasktype", 0);//这个页面这个值没什么用，主要是为了在下个页面区分是否是任务详情页面跳转的
                                intent.setClass(QfqcInfoActivity.this, PhotoBrowserActivity.class);
                                startActivity(intent);
                            }
                        });
                    } else {
                        lineartu.setVisibility(View.GONE);
                    }

                    if (!TextUtils.isEmpty(imgOk)) {
                        lineartuOK.setVisibility(View.VISIBLE);
                        final String[] sourceStrArray = imgOk.split(",");
                        for (int i = 0; i < sourceStrArray.length; i++) {
                            sourceStrArray[i] = imgulr + sourceStrArray[i];
                            System.out.println(sourceStrArray[i]);
                            Log.i("img的值", imgulr + sourceStrArray[i] + "");
                        }
                        Myhttpadapter myadapter = new Myhttpadapter(sourceStrArray, QfqcInfoActivity.this);
                        img_recycler_view_ok.setAdapter(myadapter);
                        myadapter.setItemListener(new Myhttpadapter.onRecyclerItemClickerListener() {
                            @Override
                            public void onItemClick(View view, String[] strlist, int i) {
                                Intent intent = new Intent();
                                intent.putExtra("imageUrls", sourceStrArray);
                                intent.putExtra("curImageUrl", sourceStrArray[i]);
                                intent.putExtra("tasktype", 0);//这个页面这个值没什么用，主要是为了在下个页面区分是否是任务详情页面跳转的
                                intent.setClass(QfqcInfoActivity.this, PhotoBrowserActivity.class);
                                startActivity(intent);
                            }
                        });
                    } else {
                        lineartuOK.setVisibility(View.GONE);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.btn:
                if (ZT.equals("0")) { //ZT.equals("0")  btn.getText().equals("未接受")
                    //进行网络访问，改变为接受任务状态
                    Map<String, String> map = new HashMap<>();
                    map.put("UserId", SharedPreferencedUtils.getString(QfqcInfoActivity.this, "username"));
                    map.put("ID", getIntent().getStringExtra("rwid"));
                    map.put("ZT", "0");
                    map.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
                    Log.i("更改任务状态", map.toString());
                    WebServiceUtils.callWebService(Constances.webhosturl, "N051_ReciveQCQF", map, new WebServiceUtils.WebServiceCallBack() {
                        @Override
                        public void callBack(SoapObject result) {
                            initdata(false);
                        }
                    });
                } else if (ZT.equals("3")) { //ZT.equals("3")      btn.getText().equals("未下发")
                    //进行网络访问，改变为接受任务状态
                    Map<String, String> map = new HashMap<>();
                    map.put("UserId", SharedPreferencedUtils.getString(QfqcInfoActivity.this, "username"));
                    map.put("ID", getIntent().getStringExtra("rwid"));
                    map.put("ZT", "3");
                    map.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
                    Log.i("更改任务状态", map.toString());
                    WebServiceUtils.callWebService(Constances.webhosturl, "N051_ReciveQCQF", map, new WebServiceUtils.WebServiceCallBack() {
                        @Override
                        public void callBack(SoapObject result) {
                            initdata(false);

                        }
                    });
                } else if (ZT.equals("1")) {  //ZT.equals("1")   btn.getText().equals("未完成")
                    Intent intent = new Intent(QfqcInfoActivity.this, ToqfqcActivity.class);
                    intent.putExtra("rwid", getIntent().getStringExtra("rwid"));
                    intent.putExtra("KSMC", getIntent().getStringExtra("KSMC"));
                    startActivity(intent);
                }
                break;
            case R.id.layout_back:
                finish();
                break;
            case R.id.right:
                //退出登录
                LogoutDialog dialog = new LogoutDialog(QfqcInfoActivity.this);
                dialog.setTltMsg("是否删除该任务");
                dialog.setNoStr("取消");
                dialog.setYesStr("删除");
                dialog.setYesTextColor(R.color.logout);
                dialog.setNoOnclickListener(v -> {
                    dialog.dismiss();
                });
                dialog.setYesOnclickListener(v -> {
                    dialog.dismiss();
                    Map<String, String> map = new HashMap<>();
                    map.put("id", getIntent().getStringExtra("rwid"));
                    map.put("role", "1");
                    Log.i("更改任务状态", map.toString());
                    WebServiceUtils.callWebService(Constances.webhosturl, "Del_QCQFUp", map, new WebServiceUtils.WebServiceCallBack() {
                        @Override
                        public void callBack(SoapObject result) {
                            Toast.makeText(QfqcInfoActivity.this,"该任务已删除",Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });

                });
                dialog.show();
                break;
        }
    }
}
